#!/bin/bash
export NDK_PATH=/opt/DevZone/Android/Sdk/ndk-bundle
export BUILD_PLATFORM=linux-x86_64
export TOOLCHAIN_VERSION=4.9
export ANDROID_VERSION=24

#export HOST=i686-linux-android
#export HOST_TOOL=x86
#export ARCH=x86

#export HOST=x86_64-linux-android
#export HOST_TOOL=x86_64
#export ARCH=x86_64

#export HOST=mipsel-linux-android
#export HOST_TOOL=mipsel-linux-android
#export ARCH=mips

#export HOST=mips64el-linux-android
#export HOST_TOOL=mips64el-linux-android
#export ARCH=mips64

#export HOST=aarch64-linux-android
#export HOST_TOOL=aarch64-linux-android
#export ARCH=arm64

#export HOST=arm-linux-androideabi
#export HOST_TOOL=arm-linux-androideabi
#export ARCH=arm

#export ALL_ABIS="i686-linux-android,x86,x86 x86_64-linux-android,x86_64,x86_64 mipsel-linux-android,mipsel-linux-android,mips mips64el-linux-android,mips64el-linux-android,mips64 aarch64-linux-android,aarch64-linux-android,arm64 arm-linux-androideabi,arm-linux-androideabi,arm"
#export ALL_ABIS="i686-linux-android,x86,x86"
#export ALL_ABIS="x86_64-linux-android,x86_64,x86_64"
#export ALL_ABIS="arm-linux-androideabi,arm-linux-androideabi,arm"
export ALL_ABIS="arm-linux-androideabi,arm-linux-androideabi,arm"
#export ALL_ABIS="aarch64-linux-android,aarch64-linux-android,arm64"

for i in ${ALL_ABIS}
do
IFS=","; set -- $i
export HOST=$1
export HOST_TOOL=$2
export ARCH=$3
echo "host=$HOST, host_tool=$HOST_TOOL, arch=$ARCH"

export SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-${ARCH}
export ANDROID_CFLAGS="--sysroot=${SYSROOT}"

export TOOLCHAIN=${NDK_PATH}/toolchains/${HOST_TOOL}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}
export ANDROID_INCLUDES="-I${SYSROOT}/usr/include -I${TOOLCHAIN}/include"
export CPP=${TOOLCHAIN}/bin/${HOST}-cpp
export AR=${TOOLCHAIN}/bin/${HOST}-ar
export AS=${TOOLCHAIN}/bin/${HOST}-as
export NM=${TOOLCHAIN}/bin/${HOST}-nm
export CC=${TOOLCHAIN}/bin/${HOST}-gcc
export LD=${TOOLCHAIN}/bin/${HOST}-ld
export RANLIB=${TOOLCHAIN}/bin/${HOST}-ranlib
export OBJDUMP=${TOOLCHAIN}/bin/${HOST}-objdump
export STRIP=${TOOLCHAIN}/bin/${HOST}-strip

# If building for Android 4.0.x (API level < 16) or earlier, remove `-fPIE` from
# `CFLAGS` and `-pie` from `LDFLAGS`. ${1+"$@"}
make distclean
sh configure --host=${HOST} CFLAGS="${ANDROID_INCLUDES} ${ANDROID_CFLAGS} -O3 -fPIE" CPPFLAGS="${ANDROID_INCLUDES} ${ANDROID_CFLAGS}" LDFLAGS="${ANDROID_CFLAGS} -pie" --with-simd --with-java

mkdir -p headers/${ARCH}
cp -v config.h headers/${ARCH}
cp -v jconfig.h headers/${ARCH}
cp -v jconfigint.h headers/${ARCH}

if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x86_64" ]
then
make
cp -v simd/jsimdcfg.inc headers/${ARCH}
fi
done
